package com.aplink.generic.adapter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aplink.generic.pojo.MainMenuItem;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.sg.R;

public class ExpandableListAdapter extends BaseExpandableListAdapter {
	private static class ViewHolder {
		ImageView mImageView;
		TextView mTextView;
	}
	private final HashMap<MainMenuItem, ArrayList<MainMenuItem>> mChildItem;
	private final WeakReference<Context> mContext;

	private final ArrayList<MainMenuItem> mGroupItem;

	public ExpandableListAdapter(Context context,
	        ArrayList<MainMenuItem> groupItem,
	        HashMap<MainMenuItem, ArrayList<MainMenuItem>> childItem) {
		mGroupItem = groupItem;
		mChildItem = childItem;
		mContext = new WeakReference<Context>(context);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return mChildItem.get(getGroup(groupPosition)).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (mChildItem.containsKey(getGroup(groupPosition))) {
			return mChildItem.get(getGroup(groupPosition)).size();
		}
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
	        boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater vi = (LayoutInflater) this.mContext.get()
			        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = vi.inflate(R.layout.aplink__generic_navigate_item,
			        parent, false);
			viewHolder = new ViewHolder();
			viewHolder.mImageView = (ImageView) convertView
			        .findViewById(R.id.menu_logo);
			viewHolder.mTextView = (TextView) convertView
			        .findViewById(R.id.menu_title);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		MainMenuItem mainMenuItem = mChildItem.get(
		        mGroupItem.get(groupPosition)).get(childPosition);
		viewHolder.mTextView.setText(mainMenuItem.getTitle());
		if (mainMenuItem.getIconID() != ResourcesHelper.RESOURCE_NOT_FOUND) {
			viewHolder.mImageView.setImageDrawable(this.mContext.get()
			        .getResources().getDrawable(mainMenuItem.getIconID()));
		}
		convertView.setPadding(48, 0, 0, 0);
		return convertView;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mGroupItem.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return mGroupItem.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
	        View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater vi = (LayoutInflater) this.mContext.get()
			        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = vi.inflate(R.layout.aplink__generic_navigate_item,
			        parent, false);
			viewHolder = new ViewHolder();
			viewHolder.mImageView = (ImageView) convertView
			        .findViewById(R.id.menu_logo);
			viewHolder.mTextView = (TextView) convertView
			        .findViewById(R.id.menu_title);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		MainMenuItem mainMenuItem = mGroupItem.get(groupPosition);
		viewHolder.mTextView.setText(mainMenuItem.getTitle());
		if (mainMenuItem.getIconID() != ResourcesHelper.RESOURCE_NOT_FOUND) {
			viewHolder.mImageView.setImageDrawable(this.mContext.get()
			        .getResources().getDrawable(mainMenuItem.getIconID()));
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
